import java.util.Scanner;

public class Test {
    /**
     * 二分算法
     */

    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 6, 7, 8, 12, 13, 14};
        Scanner in = new Scanner(System.in);
        System.out.println("请输入要查找的数字:");
        int target = in.nextInt();
        int result = binarySearch(arr, target);
        if (result != -1) {
            System.out.println("数字" + target + "在数组中的索引位置为:" + result);
        } else {
            System.out.println("数字" + target + "不在数组中");
        }
    }
}
